<template>
    <el-dialog
            :title="dialogParams.title"
            :visible.sync="dialogParams.visible"
            :close-on-click-modal="false"
            width="600px"
            append-to-body>
        <el-form :model="formData" :rules="rules" :disabled="dialogParams.type === 'detail'" ref="formData" label-width="100px" class="demo-fileForm">
            <el-form-item label="父级资源：" prop="parentId ">
                <el-cascader
                        v-model="formData.parentId"
                        :options="menuTree"
                        :props="{ checkStrictly: true, value: 'id', label: 'name' }"
                        placeholder="请选择父级资源"
                        clearable></el-cascader>
            </el-form-item>
            <el-form-item label="资源名称：" prop="name">
                <el-input v-model.trim="formData.name" placeholder="请输入资源名称"></el-input>
            </el-form-item>
            <el-form-item label="资源类型：" prop="type">
                <el-select v-model="formData.type" placeholder="请选择">
                    <el-option label="菜单" :value="1"></el-option>
                    <el-option label="按钮" :value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="资源URL：" prop="resUrl" :required="formData.type === 1">
                <el-input v-model.trim="formData.resUrl" placeholder="请输入资源名称"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close('formData')">取 消</el-button>
            <el-button type="primary" @click="submit('formData')">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import { addPermission, addRole, getRoleDetail, editRole} from "../../../api/systemManage";

    export default {
        name: 'addPermission',
        props: {
            dialogParams: {
                type: Object
            },
            menuTree: {
                type: Array,
                default: () => []
            }
        },
        data () {
            return {
                formData: {
                    name: '',
                    type: 1,
                    resUrl: '',
                    parentId : []
                },
                rules: {
                    name: [
                        {required: true, message: '请输入资源名称', trigger: 'blur'}
                    ],
                    type: [
                        {required: true, message: '请选择资源类型', trigger: 'change'}
                    ],
                    resUrl: [
                        { message: '请输入路径', trigger: 'blur'}
                    ]
                },
                parentResources: []
            }
        },
        created () {
            this.getDetail();
        },
        methods: {
            // 根据id获取角色详情
            getDetail () {
                if (this.dialogParams.type != 'add' && this.dialogParams.id) {
                    getRoleDetail(this.dialogParams.id).then(res => {
                        this.formData = {
                            name: res.name,
                            valid: res.valid,
                            desc: res.desc
                        }
                    }).catch()
                }
            },
            // 保存数据
            submit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        switch (this.dialogParams.type) {
                            case 'add':
                                let len = this.formData.parentId.length
                                let parentId = len ? this.formData.parentId[len - 1] : ''
                                let params = {
                                    name: this.formData.name,
                                    type: this.formData.type,
                                    resUrl: this.formData.resUrl,
                                    parentId: parentId
                                };
                                addPermission(params).then(res => {
                                    if (res) {
                                        this.$message.success('操作成功');
                                        this.$emit('getList');
                                        this.dialogParams.visible = false;
                                    }
                                }).catch();
                                break;
                        }
                    } else {
                        return false;
                    }
                });
            },
            // 关闭
            close (formName) {
                this.$refs[formName].resetFields();
                this.dialogParams.visible = false;
            }
        }
    }
</script>

<style scoped>

</style>